package com.zwr.sheller.shop.listener;


import com.zwr.sheller.core.pojo.OrderSku;
import com.zwr.sheller.core.pojo.OriginOrder;
import com.zwr.sheller.core.pojo.Sku;
import com.zwr.sheller.shop.mapper.SkuMapper;
import com.zwr.sheller.shop.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Slf4j
//@RabbitListener(queues = "dlx_queue")
@Transactional(rollbackFor = Throwable.class)
public class DLXQueueListener {
/*
    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    private OrderService orderService;
    @Autowired(required = false)
    private SkuMapper skuMapper;

    @RabbitHandler
    public void DlxHandler(String orderNum){
        System.out.println(orderNum);
        log.info(orderNum.toString());
        //通过订单编号从数据库中查询订单，查看订单状态来判断用户是否已经付款
        OriginOrder order = new OriginOrder();
        order.setOrderNum(orderNum);
        OriginOrder orderFromData = orderService.selectByFeatures(order);
        if (orderFromData.getOrderStatus() == 1){
            //订单状态为未付款需要回滚库存，修改订单状态等操作
        //1.修改订单状态为已取消
            orderFromData.setOrderStatus(5);
            orderService.updateOrderField(orderFromData);
        //2.返还库存(数据库)
            List<OrderSku> orderSkus = orderService.selectOrderSkuByFeture(orderFromData.getOrderId());
            for (OrderSku orderSku : orderSkus) {

                Sku sku = new Sku();
                sku.setSkuId(orderSku.getSkuId());
                //数据库
                Sku skuFromData = skuMapper.selectOne(sku);
                skuFromData.setSkuStock(skuFromData.getSkuStock()+orderSku.getSkuNum());
                orderService.updateSkuStock(skuFromData);

                //返回商品销量
              *//*  redisTemplate.opsForZSet().incrementScore("salesVolume:",skuFromData.getGoodsId(),-orderSku.getSkuNum());*//*


            }

        }else {
            if (orderFromData.getOrderDeliveryMethod()==3){
                orderFromData.setOrderStatus(3);
                orderService.updateOrderField(orderFromData );
            }
            //修改数据库中对应订单的状态
            orderFromData.setOrderStatus(2);
            orderService.updateOrderField(orderFromData);
        }

    }*/
}
